//η φόρμα που δείχνει τα άλμπουμ καλλιτέχνων
//author: Μάνος Μαυρέλος
package album;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import radiostation.DBManager;
import radiostation_POJOS.Album;

public class ListAlbumArtistForm extends javax.swing.JFrame {
    Album album;
    int selectedRow;
    JFrame thisFrame;
//constructor
    public ListAlbumArtistForm() {    
        //
        initComponents();
        setLocationRelativeTo(null);//κεντράρισμα της φόρμας
    }
        private void checkButtons() {//ενεργοποίηση των κουμπιών όταν επιλεχθεί άλμπουμ
        selectedRow = AlbumArtistTable.getSelectedRow();
        delAlbumArtist.setEnabled(selectedRow >= 0);
        editAlbumArtist.setEnabled(selectedRow >= 0);
        if (selectedRow >= 0) {
            album = albumList.get(AlbumArtistTable.convertRowIndexToModel(selectedRow));
        }        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadiostationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadiostationPU").createEntityManager();
        albumQuery = DBManager.getEm().createQuery("SELECT a FROM Album a WHERE a.fkArtistId != NULL");
        albumList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(albumQuery.getResultList());
        album1 = new radiostation_POJOS.Album();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        AlbumArtistTable = new javax.swing.JTable();
        newAlbumArtist = new javax.swing.JButton();
        editAlbumArtist = new javax.swing.JButton();
        delAlbumArtist = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(153, 0, 51));
        jLabel1.setText("Π ί ν α κ α ς  Album  Κ α λ λ ι τ ε χ ν ώ ν");

        AlbumArtistTable.setBackground(java.awt.Color.lightGray);
        AlbumArtistTable.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        AlbumArtistTable.setShowVerticalLines(false);
        AlbumArtistTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, albumList, AlbumArtistTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fkArtistId.artisticname}"));
        columnBinding.setColumnName("Καλλιτέχνης");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("Τίτλος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${type}"));
        columnBinding.setColumnName("Είδος");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fkMusiccompanyid.name}"));
        columnBinding.setColumnName("Εταιρεία");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${disknumber}"));
        columnBinding.setColumnName("Αριθμός δίσκων");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${releasedate}"));
        columnBinding.setColumnName("Ημερομ. ");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, album1, org.jdesktop.beansbinding.ObjectProperty.create(), AlbumArtistTable, org.jdesktop.beansbinding.BeanProperty.create("selectedElement"));
        bindingGroup.addBinding(binding);

        AlbumArtistTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AlbumArtistTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(AlbumArtistTable);
        if (AlbumArtistTable.getColumnModel().getColumnCount() > 0) {
            AlbumArtistTable.getColumnModel().getColumn(4).setMinWidth(25);
            AlbumArtistTable.getColumnModel().getColumn(4).setPreferredWidth(50);
        }

        newAlbumArtist.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        newAlbumArtist.setForeground(new java.awt.Color(153, 0, 51));
        newAlbumArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Add.png"))); // NOI18N
        newAlbumArtist.setText("Δημιουργία ");
        newAlbumArtist.setPreferredSize(new java.awt.Dimension(50, 29));
        newAlbumArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newAlbumArtistActionPerformed(evt);
            }
        });

        editAlbumArtist.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        editAlbumArtist.setForeground(new java.awt.Color(153, 0, 51));
        editAlbumArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Edit.png"))); // NOI18N
        editAlbumArtist.setText("Τροποποίηση");
        editAlbumArtist.setPreferredSize(new java.awt.Dimension(50, 29));
        editAlbumArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editAlbumArtistActionPerformed(evt);
            }
        });

        delAlbumArtist.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        delAlbumArtist.setForeground(new java.awt.Color(153, 0, 51));
        delAlbumArtist.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Delete.png"))); // NOI18N
        delAlbumArtist.setText("Διαγραφή");
        delAlbumArtist.setPreferredSize(new java.awt.Dimension(50, 29));
        delAlbumArtist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delAlbumArtistActionPerformed(evt);
            }
        });

        exit.setFont(new java.awt.Font("Calibri", 1, 12)); // NOI18N
        exit.setForeground(new java.awt.Color(153, 0, 51));
        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Log Out.png"))); // NOI18N
        exit.setText("Έξοδος");
        exit.setPreferredSize(new java.awt.Dimension(50, 29));
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(newAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(editAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(delAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(201, 201, 201)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 713, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delAlbumArtist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AlbumArtistTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AlbumArtistTableMouseClicked
    //το event για τα κουμπιά
     checkButtons();
    }//GEN-LAST:event_AlbumArtistTableMouseClicked

    private void newAlbumArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newAlbumArtistActionPerformed
        //ανοίγω την φόρμα νέο αλμπουμ καλλιτέχνη
            NewOrModifyAlbumArtistForm2 nmf = new NewOrModifyAlbumArtistForm2(null,false);
            nmf.setVisible(true);
            nmf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            nmf.setVisible(true); 
            thisFrame=this;
            thisFrame.setEnabled(false);
            thisFrame.dispose();
    }//GEN-LAST:event_newAlbumArtistActionPerformed

    private void editAlbumArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editAlbumArtistActionPerformed
        //ανοίγω την φόρμα τροποποίηση αλμπουμ καλλιτέχνη αλλά ελέγχω για επιλογή
        int s=AlbumArtistTable.getSelectedRow();              
        if (s>=0){
            Album a = albumList.get(s);
            NewOrModifyAlbumArtistForm2 nmf = new NewOrModifyAlbumArtistForm2(a,true);
            nmf.setVisible(true);
            nmf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            nmf.setVisible(true); 
            thisFrame=this;
            thisFrame.setEnabled(false);
            thisFrame.dispose();
        }
        else{
            JOptionPane.showMessageDialog(null, "Δεν επιλέξατε κάποιο άλμπουμ", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_editAlbumArtistActionPerformed

    private void delAlbumArtistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delAlbumArtistActionPerformed
        // εδώ σβήνω άλμπουμ, και επειδή είναι cascade σβήνει και τα τραγούδια
      int num=AlbumArtistTable.getSelectedRow();
      if (num>=0){//αν επέλεξα κάτι από τα άλμπουμ καλλιτέχνη
        Album a2del = albumList.get(num);
        albumList.remove(a2del);
        if (!DBManager.getEm().getTransaction().isActive()){
            DBManager.getEm().getTransaction().begin();
        }
        try{//για να είναι managed
            Album a2del2= DBManager.getEm().createNamedQuery("Album.findByAlbumId", Album.class).setParameter("albumId", a2del.getAlbumId()).getSingleResult();
            DBManager.getEm().remove(a2del2);
            DBManager.getEm().getTransaction().commit();
         }
         catch (Exception e) {
            e.printStackTrace();
            DBManager.getEm().getTransaction().rollback();
        } 
        }
        else{
            JOptionPane.showMessageDialog(null, "Πρέπει να επιλέξετε άλμπουμ πρώτα", "Σφάλμα", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_delAlbumArtistActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // κουμπί για έξοδο
        dispose();
    }//GEN-LAST:event_exitActionPerformed

    /**
     * @param args the command line arguments
     */
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTable AlbumArtistTable;
    private javax.persistence.EntityManager RadiostationPUEntityManager;
    private radiostation_POJOS.Album album1;
    private java.util.List<radiostation_POJOS.Album> albumList;
    private javax.persistence.Query albumQuery;
    private javax.swing.JButton delAlbumArtist;
    private javax.swing.JButton editAlbumArtist;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newAlbumArtist;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
